﻿using System;
using System.Data;
using System.Xml;

namespace HM.CS
{
    public class CXmlClass
    {
        //指定XML文件名  
        private string XmlFilePath = "Config.XML";

        public CXmlClass()
        {
            //检测XML配置文件是否存在  
            if (System.IO.File.Exists(XmlFilePath))
                return;

            CreateDefaultXml();
        }

        #region " ReadXML() 读取XML配置文件的参数设置，获取IP地址与默认端口"
        /// <summary>  
        /// 读取XML配置文件的参数设置，获取IP地址与默认端口
        /// </summary>  
        /// <returns></returns>  
        public DataTable ReadXML()
        {

            DataSet ds = new DataSet();
            ds.ReadXml(XmlFilePath);

            DataTable tb = new DataTable();
            tb = ds.Tables[0];

            return tb;
        }
        #endregion


        #region " WriteXML() 写XML配置文件的参数设置，保存IP地址与默认端口"
        /// <summary>  
        /// 写XML配置文件的参数设置，保存IP地址与默认端口
        /// </summary>  
        /// <returns></returns>  
        public bool WriteXML(string strIP, string strPort,string strRepeat)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                //读取XML配置文件  
                xmlDoc.Load(XmlFilePath);

                //读取XML文件节点  
                XmlNode rootNode = xmlDoc.SelectSingleNode("Network").SelectSingleNode("参数设置");
                if (rootNode == null)
                    throw (new Exception("XML配置文件信息异常"));

                //设置XML文件节点的值  
                XmlElement xIP = (XmlElement)(rootNode.SelectSingleNode("IP"));
                if (xIP == null)
                    throw (new Exception("XML配置文件信息异常"));
                xIP.InnerText = strIP;

                XmlElement xPort = (XmlElement)(rootNode.SelectSingleNode("Port"));
                if (xPort == null)
                    throw (new Exception("XML配置文件信息异常"));
                xPort.InnerText = strPort;

                XmlElement xRepeat = (XmlElement)(rootNode.SelectSingleNode("Repeat"));
                if (xPort == null)
                    throw (new Exception("XML配置文件信息异常"));
                xRepeat.InnerText = strRepeat;

                //保存XML文件  
                xmlDoc.Save(XmlFilePath);

                return true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
        #endregion

        #region " CreateDefaultXml() 创建一个默认的XML配置文件"
        /// <summary>  
        /// 创建一个默认的XML配置文件  
        /// </summary>  
        private void CreateDefaultXml()
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                //创建XML文件描述  
                XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "GB2312", null);
                xmlDoc.AppendChild(dec);
                //创建根元素  
                XmlNode root = xmlDoc.CreateNode(XmlNodeType.Element, "Network", "");
                xmlDoc.AppendChild(root);

                //添加参数设置节点  
                XmlNode parConfig = xmlDoc.CreateNode(XmlNodeType.Element, "参数设置", "");
                root.AppendChild(parConfig);

                //添加IP 
                XmlElement xIP = xmlDoc.CreateElement("IP");
                xIP.InnerText = " ";
                parConfig.AppendChild(xIP);
                //添加Port
                XmlElement xPort = xmlDoc.CreateElement("Port");
                xPort.InnerText = " ";
                parConfig.AppendChild(xPort);
                //添加允许重复扫描
                XmlElement xRepeat = xmlDoc.CreateElement("Repeat");
                xRepeat.InnerText = "1";
                parConfig.AppendChild(xRepeat);

                //保存xml文件  
                xmlDoc.Save(XmlFilePath);
            }
            catch (System.Exception ex)
            {
                throw (new Exception("创建默认XML文件失败" + ex.Message));
            }
        }
        #endregion
    }
}
